<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################


require "header.php";
require "submenu.php";

$label = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID'"));

if(!$submit) //didn't click submit button to save changes, but may have chosen a campaign to edit, reset or del
{
	if (!$cID)
		alert("No campaign specified, please try again.");
	elseif (!$sID) //no subcampaign ID, display subcampaign selector
	{
		$campaign_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID'"));

		?>
		<table width="100%">
		<tr>
			<td>
				<p class="small" align="left">Campaigns > <?=$label[0]?> > Edit SubCampaign</p>
			</td>
			<td>
				<?load_submenu($cID,$label[0])?>
			</td>
		</tr>
		</table>

		<p class="heading">
		Edit an Existing Subcampaign for <?=ucfirst($campaign_adcode[0])?>
		</p>
		<table width="500" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="white"  bordercolor="white">
		<tr>
			<td>Select a subcampaign for <?=$campaign_adcode[0]?> </td>
			<td>
				<form action="subcpgn-edit.php" method="post">
				<select name="sID">
				<?
				//selected for the sub for this action
				$subcampaigns = mysql_query("select * from adtrackz_subcampaigns where campaignID='$cID' order by adcode asc");
				while ($sub = mysql_fetch_row($subcampaigns))
						echo "<option value=\"$sub[0]\">$sub[2]</option>";
				?>
				</select></td>				
			</td>
		</tr>
		<tr>
			<td>Select an action.</td>
			<td>
				<select name="action">
				<option value=edit>Edit</option>
				<option value=reset>Reset Hits</option>
				<option value=delete>Delete</option>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">				
				<br><br><input type="submit" value="Modify Campaign" name="selectcampaign"  >
			</td>
		</tr>
		</table>
		<input type=hidden name=cID value=<?=$cID?>>
		</form>
		<?
		require "footer.php";
		exit;
	}
	elseif($action == "delete") 
	{
		if (!$confirm)
		{
			$sub_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$sID'"));

			?>
			<table width="100%">
			<tr>
				<td>
					<p class="small" align="left">Campaigns > <?=$label[0]?> > Delete SubCampaign</p>
				</td>
				<td>
					<?load_submenu($cID,$label[0])?>
				</td>
			</tr>
			</table>

			<p class=heading>
			Subcampaign Delete
			</p>

			<?
			$sub_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$sID'"));
			alert ("<p>Are you sure you want to remove <b>$sub_adcode[0]</b>? All existing data for this subcampaign will be permanently deleted.</p>");
			?>
			<br><br>
			<table align=center>
			<tr>
				<td>
					<form action=subcpgn-edit.php method=post>
					<input type=submit name=submit_delete value=" Yes ">
					<input type=hidden name=cID value=<?=$cID?>>
					<input type=hidden name=sID value=<?=$sID?>>
					<input type=hidden name=action value=delete>
					<input type=hidden name=confirm value=yes>
					</form>
				</td>
				<td>
					<form action=cpgn-edit.php method=post>
					<input type=submit name=cancel value=" No ">
					</form>
				</td>
			</tr></table>
			<?	
			require "footer.php";
			exit;
		}
		else
		{

			?>

			<table width="100%">
			<tr>
				<td>
					<p class="small" align="left">Campaigns > <?=$label[0]?> > Delete SubCampaign</p>
				</td>
				<td>
					<?load_submenu($cID,$label[0])?>
				</td>
			</tr>
			</table>

			<p class=heading>
			Subcampaign Delete
			</p>
			<?
			$sub_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where  ID='$sID'"));
			mysql_query("delete from adtrackz_subcampaigns where ID='$sID'");
			mysql_query("delete from adtrackz_clicks where subID='$sID'");
			mysql_query("delete from adtrackz_actions where subID='$sID'");
			mysql_query("delete from adtrackz_sales where subID='$sID'");
			alert("Subcampaign $sub_adcode[0] has been deleted.");			
			require "footer.php";
			exit;
		}
	}
	elseif($action == "reset") 
	{
		$sub_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$sID'"));
		if (!$confirm)
		{
			?>
			<table width="100%">
			<tr>
				<td>
					<p class="small" align="left">Campaigns > <?=$label[0]?> > Reset SubCampaign</p>
				</td>
				<td>
					<?load_submenu($cID,$label[0])?>
				</td>
			</tr>
			</table>

			<p class=heading>
			Subcampaign Reset
			</p>

			<?

			alert ("<p>Are you sure you want to delete all the clicks, actions and sales for <b>$sub_adcode[0]</b>? While the subcampaign will still exist, all existing data will be permanently deleted.</p>");
			?>
			<br><br>
			<table align=center>
			<tr>
				<td>
					<form action=subcpgn-edit.php method=post>
					<input type=submit name=submit_delete value=" Yes ">
					<input type=hidden name=cID value=<?=$cID?>>
					<input type=hidden name=sID value=<?=$sID?>>
					<input type=hidden name=action value=reset>
					<input type=hidden name=confirm value=yes>
					</form>
				</td>
				<td>
					<form action=subcpgn-edit.php method=post>
					<input type=hidden name=cID value=<?=$cID?>
					<input type=submit name=cancel value=" No ">
					</form>
				</td>
			</tr></table>
			<?	
			require "footer.php";
			exit;
		}
		else
		{
			?>
			<table width="100%">
			<tr>
				<td>
					<p class="small" align="left">Campaigns > <?=$label[0]?> > Reset SubCampaign</p>
				</td>
				<td>
					<?load_submenu($cID,$label[0])?>
				</td>
			</tr>
			</table>

			<p class=heading>
			Subcampaign Reset
			</p>

			<?
			mysql_query("delete from adtrackz_clicks where subID='$sID'");
			mysql_query("delete from adtrackz_actions where subID='$sID'");
			mysql_query("delete from adtrackz_sales where subID='$sID'");
			alert("All clicks, actions and sales have been deleted for the subcampaign $sub_adcode[0].");
			require "footer.php";
			exit;
		}
	}
}

?>

<table width="100%">
<tr>
	<td>
		<p class="small" align="left">Campaigns > <?=$label[0]?> > Edit SubCampaign</p>
	</td>
	<td>
		<?load_submenu($cID,$label[0])?>
	</td>
</tr>
</table>


<p class="heading">
Edit an Existing Campaign
</p>
<p>
To make changes to your campaign edit the fields below and click save changes.
</p>
<br>

<?
$campaigndata = mysql_fetch_row(mysql_query("select * from adtrackz_campaigns where ID='$cID'"));
$subdata = mysql_fetch_row(mysql_query("select * from adtrackz_subcampaigns where ID='$sID'"));
if ($submit)
{
	//check variables
	if (strlen($adcode) > 100)
		error("adcode must be less than 100 characters.");
	elseif (!$adcode)
		error("ad code is a required field.");
	//disabled to allow [ " - chacters (matching broad keywords in google)
	//elseif (preg_match("/(\W+|\s+)/", $adcode, $matches))
	//	error("Ad code must contain letters,digits or and underscore character only.");
	elseif (strlen($url) > 255)
		error("Url must be less than 255 characters.");
	elseif (!$url)
		error("Url is a required field.");
	elseif (!preg_match("/http:\/\//", $url))
		error("Url is not valid.");
	elseif (!preg_match("/\b\d+\b/", $cost))
		error("Cost must be in numerical format.");
	else
	{
		if ($ppc)
			$ppc = 1;
		
		$adcode = trim($adcode);
		$adcode = strtolower($adcode);		
		//strip & and | characters
		$adcode = str_replace("&","",$adcode);
		$adcode = str_replace("|","",$adcode);

		mysql_query("update adtrackz_subcampaigns set adcode='$adcode', url='$url', cost='$cost', description='$description', ppc='$ppc' where ID='$sID'");

		$adcode = stripslashes($adcode);

		alert ("$adcode has been updated successfully.");
	}
}
else  //set variables that can be edited only
{
	$adcode = $subdata[2];
	$url = $subdata[3];
	$cost = $subdata[4];
	$ppc = $subdata[6];
	$description = $subdata[5];
}

//set other variables 
$group = $campaigndata[1];
$purpose = $campaigndata[8];
$startdatest = strtotime($campaigndata[5]);
$startdate = date("D M j, Y", $startdatest);
$enddatest = strtotime($campaigndata[6]);
$enddate = date("D M j, Y", $enddatest);

?>

<br>
<form action="subcpgn-edit.php" method="post">
<table width="500" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="white"  bordercolor="white">
<tr>
	<td valign="top">Ad Code</td>
	<td valign="top">
		<input type="text" size="20" name="adcode" value='<?=$adcode?>'> 		
	</td>
</tr>
<tr>
	<td valign="top">Url</td>
	<td valign="top">
		<input type="text" size="30"  name="url" value="<?=$url?>">
	</td>
</tr> 
<tr>
	<td>Group:
	<td>
		<?
		if (!$group)
			echo "default";
		else
			echo $group;
		?>
	</td>
</tr>
<tr>
	<td>Purpose:
	<td>
		<?
		if ($purpose == "all")
			echo "Clicks, Actions & Sales";
		elseif ($purpose == "clicks")
			echo "Clicks Only";
		elseif ($purpose == "actions")
			echo "Clicks & Actions";
		elseif ($purpose == "sales")
			echo "Clicks & Sales";
		elseif ($purpose == "affiliate")
			echo "Clicks, Sales, Actions on Affiliate Websites";
		?>
	</td>
</tr>
<tr>
	<td valign="top">Cost</td>
	<td valign="top">
		<?if (!$cost)
		{?>
			<input type="text" size="6" maxlength="10" name="cost" value="0.00"> 
		<?}
		else
		{?>
			<input type="text" size="6" maxlength="10" name="cost" value="<?=$cost?>"> 
		<?}?>
	</td>
</tr>
<tr>
	<td valign="top">Cost Type</td>
	<td valign="top">
		<?
		if ($ppc)
			$ppc_s = "checked";
		?>
		<input type="checkbox" name="ppc" <?=$ppc_s?>> PPC<br>
	</td>
</tr> 
<tr>
	<td valign="top">Start Date</td>
	<td valign="top">
		<?=$startdate?>
	</td>
</tr>
<tr>
	<td valign="top">End Date</td>
	<td valign="top">
		<?=$enddate?>
	</td>
</tr>
<tr>
	<td valign="top">Description</td>
	<td valign="top">
		<textarea rows="3" cols="20" name="description"><?=$description?></textarea>
	</td>
</tr>
</table>

<br><br>
<center>
<input type="hidden" name="cID" value="<?=$cID?>">
<input type="hidden" name="sID" value="<?=$sID?>">
<input type="submit" name="submit" value="Save Changes"  >
</center></form><br>


<?

require "footer.php";

?>